<?php

	class DataListManager{
		
		private $tablename;
		private $id_values; // az id értékeket tartalmazó tömb, sorszámmal van indexelve
		private $current_id;
		
		public $where;
		public $length;
		public $limit;
		public $order;
		
		public function __construct($table, $where='1', $order='', $limit=''){
			mysql_ping() or ex_throw(new Exception("DataListManager::__construct($table, $where, $order, $limit) Mysql szerver kapcsolat meghalt :'("));
			$this->tablename = $table;
			$this->limit = $limit;
			$this->order = $order;
			
			$this->current_id = 0;
			
			if(!empty($where) and $where!=1){
				$this->where = $where;
			}
			else{
				$this->where = '1';
			}
			
			$sql='SELECT id FROM '.$this->tablename.' WHERE '.$this->where.' ';
			if(!empty($order)){
				$sql.='ORDER BY '.$order.' ';
			}
			if(!empty($limit)){
				$sql.='LIMIT '.$limit.' ';
			}
			$q=mysql_query($sql) or ex_throw(new Exception("DataListManager::__construct( $table, $where, $order, $limit ) Nem létezik a ".$this->tablename.' tábla, vagy egyéb MYSQL hiba. mysql_query("'.$sql.'")'."\n".mysql_error()));
			$this->id_values = array();
			while($s=mysql_fetch_assoc($q)){
				$this->id_values[] = $s['id'];
			}
			$this->length = count($this->id_values);
			
		}

		public function getLength(){
			return $this->length;
		}
		
		public function manager($rownum){ // visszaadja a megadott sorszámú (VIGYÁZZ nem = az id-vel) sor managerét
			if(!isset($this->id_values[$rownum])){
				throw new Exception("DataListManager::manager() a $rownum sor nem létezik.");
			}
			
			return new DataRowManager($this->tablename, $this->id_values[$rownum]);
		}
		
		public function next_row(){ // visszaadja a következő sor managerét
			if($this->current_id<count($this->id_values)){
				return new DataRowManager($this->tablename, $this->id_values[$this->current_id++]);
			}
			else {
				
				return false;
			}
		}
		
		public function nextRow(){ // alias for compatibility
			return $this->next_row();
		}
		
		public function restart(){
			$this->__construct($this->tablename, $this->where, $this->order, $this->limit);
		}
		
		
		public function truncate(){
			$s = "DELETE FROM ".$this->tablename.' WHERE '.$this->where;
			if($this->order!=''){
				$s .= ' ORDER BY '.$this->order;
			}
			if($this->limit!=''){
				$s .= ' LIMIT '.$this->limit;
			}
			return mysql_query($s);
		}
		
		
	}
?>
